using System;
using UnityEngine;

[AddComponentMenu("2D Toolkit/Sprite/tk2dProgress")]
[RequireComponent(typeof(MeshRenderer))]
[RequireComponent(typeof(MeshFilter))]
[ExecuteInEditMode]
public class tk2dProgress : tk2dBaseSprite
{
	public float Percent
	{
		get
		{
			return this._percent;
		}
		set
		{
			this._percent = value;
			this.UpdateVertices();
		}
	}

	public bool IsLeftDraw
	{
		get
		{
			return this._isLeftDraw;
		}
		set
		{
			if (value != this._isLeftDraw)
			{
				this._isLeftDraw = value;
				this.Build();
			}
		}
	}

	private new void Awake()
	{
		base.Awake();
		this.mesh = new Mesh();
		this.mesh.hideFlags = HideFlags.DontSave;
		base.GetComponent<MeshFilter>().mesh = this.mesh;
		if (base.Collection)
		{
			if (this._spriteId < 0 || this._spriteId >= base.Collection.Count)
			{
				this._spriteId = 0;
			}
			this.Build();
		}
	}

	protected void OnDestroy()
	{
		if (this.mesh)
		{
			UnityEngine.Object.DestroyImmediate(this.mesh);
		}
		if (this.meshColliderMesh)
		{
			UnityEngine.Object.Destroy(this.meshColliderMesh);
		}
	}

	public override void Build()
	{
		tk2dSpriteDefinition tk2dSpriteDefinition = this.collectionInst.spriteDefinitions[base.spriteId];
		this.meshVertices = new Vector3[tk2dSpriteDefinition.positions.Length];
		this.meshColors = new Color32[tk2dSpriteDefinition.positions.Length];
		this.meshNormals = new Vector3[0];
		this.meshTangents = new Vector4[0];
		if (tk2dSpriteDefinition.normals != null && tk2dSpriteDefinition.normals.Length > 0)
		{
			this.meshNormals = new Vector3[tk2dSpriteDefinition.normals.Length];
		}
		if (tk2dSpriteDefinition.tangents != null && tk2dSpriteDefinition.tangents.Length > 0)
		{
			this.meshTangents = new Vector4[tk2dSpriteDefinition.tangents.Length];
		}
		base.SetPositions(this.meshVertices, this.meshNormals, this.meshTangents);
		base.SetColors(this.meshColors);
		if (this.mesh == null)
		{
			this.mesh = new Mesh();
			this.mesh.hideFlags = HideFlags.DontSave;
			base.GetComponent<MeshFilter>().mesh = this.mesh;
		}
		this.mesh.Clear();
		this.mesh.vertices = this.GetVertices(tk2dSpriteDefinition.positions, this._scale);
		this.mesh.normals = this.meshNormals;
		this.mesh.tangents = this.meshTangents;
		this.mesh.colors32 = this.meshColors;
		bool isFlipped = tk2dSpriteDefinition.flipped == tk2dSpriteDefinition.FlipMode.Tk2d || tk2dSpriteDefinition.flipped == tk2dSpriteDefinition.FlipMode.TPackerCW;
		this.mesh.uv = this.SetUvs(tk2dSpriteDefinition.uvs, isFlipped);
		this.mesh.triangles = tk2dSpriteDefinition.indices;
		this.mesh.bounds = tk2dBaseSprite.AdjustedMeshBounds(base.GetBounds(), this.renderLayer);
		this.UpdateMaterial();
		this.CreateCollider();
	}

	public static tk2dSprite AddComponent(GameObject go, tk2dSpriteCollectionData spriteCollection, int spriteId)
	{
		return tk2dBaseSprite.AddComponent<tk2dSprite>(go, spriteCollection, spriteId);
	}

	public static tk2dSprite AddComponent(GameObject go, tk2dSpriteCollectionData spriteCollection, string spriteName)
	{
		return tk2dBaseSprite.AddComponent<tk2dSprite>(go, spriteCollection, spriteName);
	}

	public static GameObject CreateFromTexture(Texture texture, tk2dSpriteCollectionSize size, Rect region, Vector2 anchor)
	{
		return tk2dBaseSprite.CreateFromTexture<tk2dSprite>(texture, size, region, anchor);
	}

	protected override void UpdateGeometry()
	{
		this.UpdateGeometryImpl();
	}

	protected override void UpdateColors()
	{
		this.UpdateColorsImpl();
	}

	protected override void UpdateVertices()
	{
		this.UpdateVerticesImpl();
	}

	protected void UpdateColorsImpl()
	{
		if (this.mesh == null || this.meshColors == null || this.meshColors.Length == 0)
		{
			return;
		}
		base.SetColors(this.meshColors);
		this.mesh.colors32 = this.meshColors;
	}

	protected void UpdateVerticesImpl()
	{
		tk2dSpriteDefinition tk2dSpriteDefinition = this.collectionInst.spriteDefinitions[base.spriteId];
		if (this.mesh == null || this.meshVertices == null || this.meshVertices.Length == 0)
		{
			return;
		}
		if (tk2dSpriteDefinition.normals.Length != this.meshNormals.Length)
		{
			this.meshNormals = ((tk2dSpriteDefinition.normals == null || tk2dSpriteDefinition.normals.Length <= 0) ? new Vector3[0] : new Vector3[tk2dSpriteDefinition.normals.Length]);
		}
		if (tk2dSpriteDefinition.tangents.Length != this.meshTangents.Length)
		{
			this.meshTangents = ((tk2dSpriteDefinition.tangents == null || tk2dSpriteDefinition.tangents.Length <= 0) ? new Vector4[0] : new Vector4[tk2dSpriteDefinition.tangents.Length]);
		}
		base.SetPositions(this.meshVertices, this.meshNormals, this.meshTangents);
		this.mesh.vertices = this.GetVertices(tk2dSpriteDefinition.positions, this._scale);
		this.mesh.normals = this.meshNormals;
		this.mesh.tangents = this.meshTangents;
		bool isFlipped = tk2dSpriteDefinition.flipped == tk2dSpriteDefinition.FlipMode.Tk2d || tk2dSpriteDefinition.flipped == tk2dSpriteDefinition.FlipMode.TPackerCW;
		this.mesh.uv = this.SetUvs(tk2dSpriteDefinition.uvs, isFlipped);
		this.mesh.bounds = tk2dBaseSprite.AdjustedMeshBounds(base.GetBounds(), this.renderLayer);
	}

	protected void UpdateGeometryImpl()
	{
		if (this.mesh == null)
		{
			return;
		}
		tk2dSpriteDefinition tk2dSpriteDefinition = this.collectionInst.spriteDefinitions[base.spriteId];
		if (this.meshVertices == null || this.meshVertices.Length != tk2dSpriteDefinition.positions.Length)
		{
			this.meshVertices = new Vector3[tk2dSpriteDefinition.positions.Length];
			this.meshNormals = ((tk2dSpriteDefinition.normals == null || tk2dSpriteDefinition.normals.Length <= 0) ? new Vector3[0] : new Vector3[tk2dSpriteDefinition.normals.Length]);
			this.meshTangents = ((tk2dSpriteDefinition.tangents == null || tk2dSpriteDefinition.tangents.Length <= 0) ? new Vector4[0] : new Vector4[tk2dSpriteDefinition.tangents.Length]);
			this.meshColors = new Color32[tk2dSpriteDefinition.positions.Length];
		}
		base.SetPositions(this.meshVertices, this.meshNormals, this.meshTangents);
		base.SetColors(this.meshColors);
		this.mesh.Clear();
		this.mesh.vertices = this.GetVertices(tk2dSpriteDefinition.positions, this._scale);
		this.mesh.normals = this.meshNormals;
		this.mesh.tangents = this.meshTangents;
		this.mesh.colors32 = this.meshColors;
		bool isFlipped = tk2dSpriteDefinition.flipped == tk2dSpriteDefinition.FlipMode.Tk2d || tk2dSpriteDefinition.flipped == tk2dSpriteDefinition.FlipMode.TPackerCW;
		this.mesh.uv = this.SetUvs(tk2dSpriteDefinition.uvs, isFlipped);
		this.mesh.bounds = tk2dBaseSprite.AdjustedMeshBounds(base.GetBounds(), this.renderLayer);
		this.mesh.triangles = tk2dSpriteDefinition.indices;
	}

	protected Vector3[] GetVertices(Vector3[] iPos, Vector3 scale)
	{
		return new Vector3[]
		{
			Vector3.Scale(new Vector3(Mathf.Lerp(iPos[0].x, iPos[3].x, 0f), Mathf.Lerp(iPos[0].y, iPos[3].y, 0f)), scale),
			Vector3.Scale(new Vector3(Mathf.Lerp(iPos[0].x, iPos[3].x, this.Percent / 100f), Mathf.Lerp(iPos[0].y, iPos[3].y, 0f)), scale),
			Vector3.Scale(new Vector3(Mathf.Lerp(iPos[0].x, iPos[3].x, 0f), Mathf.Lerp(iPos[0].y, iPos[3].y, 1f)), scale),
			Vector3.Scale(new Vector3(Mathf.Lerp(iPos[0].x, iPos[3].x, this.Percent / 100f), Mathf.Lerp(iPos[0].y, iPos[3].y, 1f)), scale)
		};
	}

	protected Vector2[] SetUvs(Vector2[] iUv, bool isFlipped)
	{
		Vector2[] array = new Vector2[4];
		if (this._isLeftDraw)
		{
			if (isFlipped)
			{
				array[0] = iUv[0];
				array[1] = new Vector2(iUv[1].x, Mathf.Lerp(iUv[0].y, iUv[3].y, this.Percent / 100f));
				array[2] = iUv[2];
				array[3] = new Vector2(iUv[3].x, Mathf.Lerp(iUv[0].y, iUv[3].y, this.Percent / 100f));
			}
			else
			{
				array[0] = iUv[0];
				array[1] = new Vector2(Mathf.Lerp(iUv[0].x, iUv[3].x, this.Percent / 100f), iUv[0].y);
				array[2] = iUv[2];
				array[3] = new Vector2(Mathf.Lerp(iUv[0].x, iUv[3].x, this.Percent / 100f), iUv[2].y);
			}
		}
		else if (isFlipped)
		{
			array[0] = new Vector2(iUv[0].x, Mathf.Lerp(iUv[0].y, iUv[3].y, 1f - this.Percent / 100f));
			array[1] = iUv[1];
			array[2] = new Vector2(iUv[2].x, Mathf.Lerp(iUv[0].y, iUv[3].y, 1f - this.Percent / 100f));
			array[3] = iUv[3];
		}
		else
		{
			array[0] = new Vector2(Mathf.Lerp(iUv[0].x, iUv[3].x, 1f - this.Percent / 100f), iUv[0].y);
			array[1] = iUv[1];
			array[2] = new Vector2(Mathf.Lerp(iUv[0].x, iUv[3].x, 1f - this.Percent / 100f), iUv[2].y);
			array[3] = iUv[3];
		}
		return array;
	}

	protected override void UpdateMaterial()
	{
		if (base.GetComponent<Renderer>().sharedMaterial != this.collectionInst.spriteDefinitions[base.spriteId].materialInst)
		{
			base.GetComponent<Renderer>().material = this.collectionInst.spriteDefinitions[base.spriteId].materialInst;
		}
	}

	protected override int GetCurrentVertexCount()
	{
		if (this.meshVertices == null)
		{
			return 0;
		}
		return this.meshVertices.Length;
	}

	public override void ForceBuild()
	{
		base.ForceBuild();
		base.GetComponent<MeshFilter>().mesh = this.mesh;
	}

	public override void ReshapeBounds(Vector3 dMin, Vector3 dMax)
	{
		tk2dSpriteDefinition currentSprite = base.CurrentSprite;
		Vector3 a = Vector3.Scale(currentSprite.untrimmedBoundsData[0] - 0.5f * currentSprite.untrimmedBoundsData[1], this._scale);
		Vector3 a2 = Vector3.Scale(currentSprite.untrimmedBoundsData[1], this._scale);
		Vector3 vector = a2 + dMax - dMin;
		vector.x /= currentSprite.untrimmedBoundsData[1].x;
		vector.y /= currentSprite.untrimmedBoundsData[1].y;
		Vector3 b = new Vector3((!Mathf.Approximately(this._scale.x, 0f)) ? (a.x * vector.x / this._scale.x) : 0f, (!Mathf.Approximately(this._scale.y, 0f)) ? (a.y * vector.y / this._scale.y) : 0f);
		Vector3 position = a + dMin - b;
		position.z = 0f;
		base.transform.position = base.transform.TransformPoint(position);
		base.scale = new Vector3(vector.x, vector.y, this._scale.z);
	}

	private Mesh mesh;

	private Vector3[] meshVertices;

	private Vector3[] meshNormals;

	private Vector4[] meshTangents;

	private Color32[] meshColors;

	private float _percent;

	[SerializeField]
	private bool _isLeftDraw;
}
